package xiaohu.day7.HJ77;


import java.util.*;

public class Main2 {

    private static boolean[] used = null;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        used = new boolean[n + 1];

        int[] nums = new int[n];
        int[] tmp = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }

        dfs(n, nums, tmp, 0);

        sc.close();
    }

    private static void dfs(int n, int[] nums, int[] tmp, int step) {
        if (step == nums.length) {
            if (isValid(nums, tmp)) {
                System.out.println(printResult(tmp));
            }
            return;
        }

        for (int i = 1; i <= n; i++) {
            if (used[i]) {
                continue;
            }

            used[i] = true;
            tmp[step] = i;
            dfs(n, nums, tmp, step + 1);
            used[i] = false;
        }
    }

    private static boolean isValid(int[] nums, int[] tmp) {

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }

        Set<Integer> set = new HashSet<>();
        int idx = 0;
        for (int i = 0; i < tmp.length; i++) {
            while (!set.contains(tmp[i])) {
                set.add(nums[idx]);
                idx++;
            }

            for (int j = map.get(tmp[i]) + 1; j < idx; j++) {
                if (set.contains(nums[j])) {
                    return false;
                }
            }
            set.remove(tmp[i]);
        }
        return true;
    }

    private static String printResult(int[] tmp) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tmp.length - 1; i++) {
            sb.append(tmp[i]).append(" ");
        }
        sb.append(tmp[tmp.length - 1]);
        return sb.toString();
    }
}

